package com.niiwoo.civet.mobile.controller.intelligenceInvest;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.mobile.vo.request.intelligenceInvest.IntelligenceDetailListRequestVO;
import com.niiwoo.civet.mobile.vo.request.intelligenceInvest.IntelligenceOrderDetailRequestVO;
import com.niiwoo.civet.mobile.vo.request.intelligenceInvest.IntelligenceOrderListRequestVO;
import com.niiwoo.civet.mobile.vo.response.intelligenceInvest.IntelligenceDetailListResponseVO;
import com.niiwoo.civet.mobile.vo.response.intelligenceInvest.IntelligenceInvestOrder;
import com.niiwoo.civet.mobile.vo.response.intelligenceInvest.IntelligenceOrderDetailResponseVO;
import com.niiwoo.civet.mobile.vo.response.intelligenceInvest.IntelligenceOrderListResponseVO;
import com.niiwoo.civet.trade.dto.request.intelligenceInvest.IntelligenceDetailListRequestDTO;
import com.niiwoo.civet.trade.dto.request.intelligenceInvest.IntelligenceOrderDetailRequestDTO;
import com.niiwoo.civet.trade.dto.request.intelligenceInvest.IntelligenceOrderListRequestDTO;
import com.niiwoo.civet.trade.dto.response.InvestOrderMaxandMinDateResponseDTO;
import com.niiwoo.civet.trade.dto.response.intelligenceInvest.IntelligenceDetailListResponseDTO;
import com.niiwoo.civet.trade.dto.response.intelligenceInvest.IntelligenceInvestOrderDTO;
import com.niiwoo.civet.trade.dto.response.intelligenceInvest.IntelligenceOrderDetailResponseDTO;
import com.niiwoo.civet.trade.service.intelligenceInvest.IntelligenceOrderDubboService;
import com.niiwoo.civet.trade.service.invest.InvestOrderDubboService;
import com.niiwoo.tripod.provider.dto.response.PageResponseDTO;
import com.niiwoo.tripod.web.util.PrincipalContext;
import com.niiwoo.tripod.web.vo.PageResponseVO;
import com.niiwoo.tripod.web.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by HuangMin on 2018/10/27.
 */
@Slf4j
@RestController
@Api(tags = "V5.5.5--智能投订单", description = "/intelligenceOrder")
@RequestMapping("/intelligenceOrder/555")
public class IntelligenceOrderController {

    @Reference(version = "1.0.0")
    private InvestOrderDubboService investOrderDubboService;
    @Reference(version = "1.0.0")
    private IntelligenceOrderDubboService intelligenceOrderDubboService;

    @PostMapping("/intelligenceOrderList")
    @ApiOperation("智能投包订单列表-黄敏")
    public Result<IntelligenceOrderListResponseVO> intelligenceOrderList(@RequestBody @Valid IntelligenceOrderListRequestVO requestVO) {
        String userId = PrincipalContext.getUserId();
        log.info("intelligenceOrderList IntelligenceOrderListRequestVO:{}, userId={}", JSON.toJSON(requestVO), userId);

        IntelligenceOrderListRequestDTO requestDTO = new IntelligenceOrderListRequestDTO();
        requestDTO.setUserId(userId);
        requestDTO.setPageNumber(requestVO.getPageNumber());
        requestDTO.setPageSize(requestVO.getPageSize());
        requestDTO.setType(requestVO.getType() == null ? 0 : requestVO.getType());
        requestDTO.setStatus(requestVO.getStatus());
        if (requestVO.getStartTime() != null && requestVO.getStartTime().getTime() != 0L) {
            requestDTO.setStartTime(requestVO.getStartTime());
        }
        if (requestVO.getEndTime() != null && requestVO.getEndTime().getTime() != 0L) {
            requestDTO.setEndTime(requestVO.getEndTime());
        }

        IntelligenceOrderListResponseVO responseVO = new IntelligenceOrderListResponseVO();
        PageResponseDTO<IntelligenceInvestOrderDTO> pageResponseDTO = intelligenceOrderDubboService.intelligenceOrderList(requestDTO);
        PageResponseVO<IntelligenceInvestOrder> pageResponseVO = new PageResponseVO<>();
        pageResponseVO.setTotalCount(pageResponseDTO.getTotalCount());
        pageResponseVO.setTotalPage(pageResponseDTO.getTotalPage());
        if (pageResponseDTO.getTotalCount() > 0) {
            List<IntelligenceInvestOrder> investOrderList = new ArrayList<>();
            pageResponseDTO.getItems().forEach(responseDTO ->{
                IntelligenceInvestOrder investOrder = new IntelligenceInvestOrder();
                investOrder.setPackageInvestOrderId(responseDTO.getPackageInvestOrderId());
                investOrder.setTitle(responseDTO.getTitle());
                investOrder.setDeadline(responseDTO.getDeadline());
                investOrder.setDeadlineUnit(responseDTO.getDeadlineUnit());
                investOrder.setBorrowRate(responseDTO.getBorrowRate());
                investOrder.setAddRate(responseDTO.getAddRate());
                investOrder.setStatus(responseDTO.getStatus());
                investOrder.setAmount(responseDTO.getAmount());
                investOrder.setInterest(responseDTO.getInterest());
                investOrder.setTime(responseDTO.getTime());
                investOrder.setPackageType(responseDTO.getPackageType());
                investOrder.setIntelligenceCount(responseDTO.getIntelligenceCount());
                investOrderList.add(investOrder);
            });
            pageResponseVO.setItems(investOrderList);
            pageResponseVO.setTotalPage(pageResponseDTO.getTotalPage());
            pageResponseVO.setTotalCount(pageResponseDTO.getTotalCount());
        }else{
            responseVO.setMaxInterestRate(investOrderDubboService.getMaxRate()); // 最大利率
        }
        responseVO.setPageResponseVO(pageResponseVO);
        responseVO.setOrdersNum(pageResponseDTO.getTotalCount());

        // 最大最小日期
        InvestOrderMaxandMinDateResponseDTO dateResponseDTO = intelligenceOrderDubboService.getMyIntelligenceInvestOrderMaxAndMinDate(userId, requestDTO.getType());
        if (dateResponseDTO != null && dateResponseDTO.getMaxTime() != null) {
            responseVO.setMaxDate(dateResponseDTO.getMaxTime());
        }
        if (dateResponseDTO != null && dateResponseDTO.getMinTime() != null) {
            responseVO.setMinDate(dateResponseDTO.getMinTime());
        }
        return Result.with(responseVO);
    }

    @PostMapping("/intelligenceOrderDetail")
    @ApiOperation("智能投包订单详情-黄敏")
    public Result<IntelligenceOrderDetailResponseVO> intelligenceOrderDetail(@RequestBody @Valid IntelligenceOrderDetailRequestVO requestVO) {
        String userId = PrincipalContext.getUserId();
        log.info("intelligenceOrderDetail IntelligenceOrderDetailRequestVO:{}, userId:{}", JSON.toJSON(requestVO), userId);

        IntelligenceOrderDetailResponseVO responseVO = null;
        IntelligenceOrderDetailRequestDTO requestDTO = new IntelligenceOrderDetailRequestDTO();
        requestDTO.setPackageInvestOrderId(requestVO.getPackageInvestOrderId());
        requestDTO.setUserId(userId);
        IntelligenceOrderDetailResponseDTO responseDTO = intelligenceOrderDubboService.intelligenceOrderDetail(requestDTO);
        if(responseDTO != null) {
            responseVO = new IntelligenceOrderDetailResponseVO();
            responseVO.setPackageInvestOrderId(responseDTO.getPackageInvestOrderId());
            responseVO.setPackageId(responseDTO.getPackageId());
            responseVO.setTitle(responseDTO.getTitle());
            responseVO.setDeadline(responseDTO.getDeadline());
            responseVO.setDeadlineUnit(responseDTO.getDeadlineUnit());
            responseVO.setBorrowRate(responseDTO.getBorrowRate());
            responseVO.setAddRate(responseDTO.getAddRate());
            responseVO.setTotalRepayPriInter(responseDTO.getTotalRepayPriInter());
            responseVO.setRepayAmount(responseDTO.getRepayAmount());
            responseVO.setAmount(responseDTO.getAmount());

            responseVO.setStatus(responseDTO.getStatus());
            responseVO.setPackageType(responseDTO.getPackageType());
            responseVO.setRepaymentType(responseDTO.getRepaymentType());
            responseVO.setPrize(responseDTO.getPrize());
            responseVO.setRepayTime(responseDTO.getRepayTime());
            responseVO.setSubmitTime(responseDTO.getSubmitTime());
            responseVO.setMessage(responseDTO.getMessage());
            responseVO.setEndTime(responseDTO.getEndTime());

            responseVO.setReferenceIncome(responseDTO.getReferenceIncome());
            responseVO.setInterest(responseDTO.getInterest());
            responseVO.setPenaltyFee(responseDTO.getPenaltyFee());
            responseVO.setPlatformAddInterest(responseDTO.getPlatformAddInterest());
            responseVO.setPrizeAddInterest(responseDTO.getPrizeAddInterest());
         responseVO.setIntelligenceCount(responseDTO.getIntelligenceCount());
        }
        return Result.with(responseVO);
    }

    @PostMapping("/intelligenceDetailList")
    @ApiOperation("智能投包明细订单列表-黄敏")
    public Result<PageResponseVO<IntelligenceDetailListResponseVO>> intelligenceDetailList(@RequestBody @Valid IntelligenceDetailListRequestVO requestVO) {
        String userId = PrincipalContext.getUserId();
        log.info("intelligenceDetailList IntelligenceDetailListRequestVO:{}, userId:{}", JSON.toJSON(requestVO), userId);

        IntelligenceDetailListRequestDTO requestDTO = new IntelligenceDetailListRequestDTO();
        requestDTO.setUserId(userId);
        requestDTO.setPageSize(requestVO.getPageSize());
        requestDTO.setPageNumber(requestVO.getPageNumber());
        requestDTO.setPackageInvestOrderId(requestVO.getPackageInvestOrderId());
        PageResponseDTO<IntelligenceDetailListResponseDTO> pageResponseDTO = intelligenceOrderDubboService.intelligenceDetailList(requestDTO);
        PageResponseVO<IntelligenceDetailListResponseVO> pageResponseVO = new PageResponseVO<>();
        pageResponseVO.setTotalPage(pageResponseDTO.getTotalPage());
        pageResponseVO.setTotalCount(pageResponseDTO.getTotalCount());
        if(pageResponseDTO.getTotalCount() > 0){
            List<IntelligenceDetailListResponseVO> responseVOList = new ArrayList<>();
            pageResponseDTO.getItems().forEach(responseDTO ->{
                IntelligenceDetailListResponseVO responseVO = new IntelligenceDetailListResponseVO();
                responseVO.setProjectId(responseDTO.getProjectId());
                responseVO.setOrderId(responseDTO.getOrderId());
                responseVO.setHeadImg(responseDTO.getHeadImg());
                responseVO.setNickName(responseDTO.getNickName());
                responseVO.setDeadline(responseDTO.getDeadline());
                responseVO.setDeadlineUnit(responseDTO.getDeadlineUnit());
                responseVO.setBorrowRate(responseDTO.getBorrowRate());
                responseVO.setAddRate(responseDTO.getAddRate());
                responseVO.setStatus(responseDTO.getStatus());
                responseVO.setAmount(responseDTO.getAmount());
                responseVO.setInterest(responseDTO.getInterest());
                responseVO.setCalInterTime(responseDTO.getCalInterTime());
                responseVO.setExpireTime(responseDTO.getExpireTime());
                responseVO.setEndTime(responseDTO.getEndTime());
                responseVO.setPackageType(responseDTO.getPackageType());
                responseVO.setOverdueMsg(responseDTO.getOverdueMsg());
                responseVO.setIsEnterpriseProject(responseDTO.getIsEnterpriseProject());
                responseVOList.add(responseVO);
            });
            pageResponseVO.setItems(responseVOList);
        }

        return Result.with(pageResponseVO);
    }

}
